#pragma once

#include <iostream>
#include <string>
#include <sys/time.h>

namespace ns_log
{
    // 等级
    enum
    {
        INFO,
        DEBUG,
        WARNING,
        ERROR,
        FATAL
    };

#define LOG(level) Log(#level, __FILE__, __LINE__) // # 数字转字符串

    static inline std::string getFormatTime()
    {
        struct timeval tv;
        struct timezone tz;
        gettimeofday(&tv, &tz);
        struct tm *current_time = localtime(&tv.tv_sec);
        char formatTime[24];
        sprintf(formatTime,
                "%04d-%02d-%02d %02d:%02d:%02d.%03d",
                current_time->tm_year + 1900,
                current_time->tm_mon + 1,
                current_time->tm_mday,
                current_time->tm_hour,
                current_time->tm_min,
                current_time->tm_sec,
                tv.tv_usec / 1000);
        return formatTime;
    }
    // time level [filename][line][message]
    inline std::ostream &Log(const std::string level, const std::string filename, int line)
    {

        std::string message = getFormatTime();
        message += " " + level + " [" + filename + "] [" + std::to_string(line) + "] ";
        std::cout << message;

        return std::cout;
    }
}